//
//  TopicModel.m
//  DaJiang
//
//  Created by sally on 16/10/25.
//  Copyright © 2016年 jiniu. All rights reserved.
//

#import "ExpertInterface.h"
#import "OrderInterface.h"
#import "TopicModel.h"

@implementation TopicModel

+ (BOOL)propertyIsOptional:(NSString *)propertyName {
  return YES;
}

+ (JSONKeyMapper *)keyMapper {
  return [[JSONKeyMapper alloc] initWithDictionary:@{ @"id" : @"topicid" }];
}

//管理话题列表
+ (void)queryTopicListWithCursor:(NSInteger)cursor
                      completion:(void (^)(NSMutableArray *topicList))completion {
  [self cleanLastError];

  completion_t _completion = ^(NSDictionary *resultObject, NSInteger code, NSString *description) {
    if (code == SUCCESS_RESULT) {
      NSMutableArray *retMArray = [NSMutableArray array];
      NSDictionary *listResultDic = resultObject[DicKeyList];
      if (listResultDic && ![listResultDic isKindOfClass:[NSNull class]] && listResultDic.count > 0) {
        for (id item in listResultDic) {
          TopicModel *topicModel = [[TopicModel alloc] initWithDictionary:item error:nil];
          [retMArray addObject:topicModel];
        }
      }
      completion(retMArray);

    } else {
      [self setLastError:[NSError errorWithDomain:description code:code userInfo:nil]];
      completion(nil);
    }

  };

  error_t _error = ^(NSError *error) {
    [self setLastError:error];
    completion(nil);
  };

  ExpertInterface *interface = [[ExpertInterface alloc] init];
  [interface queryTopicListWithCursor:cursor completion:_completion error:_error];
}

//修改话题
+ (void)addOrUpdateTopicWithTopicId:(NSInteger)topicId
                          topicName:(NSString *)topicName
                         timeLength:(NSString *)timeLength
                          topicDesc:(NSString *)topicDesc
                         topicPrice:(NSString *)topicPrice
                         completion:(void (^)(TopicModel *topicModel))completion {
  [self cleanLastError];

  completion_t _completion = ^(NSDictionary *resultObject, NSInteger code, NSString *description) {
    TopicModel *topicModel = nil;
    NSMutableArray *resultArr = (NSMutableArray *) resultObject;
    if (code == SUCCESS_RESULT) {
      if (topicId > GTopicIdInit) {
        topicModel = [[TopicModel alloc] initWithDictionary:resultArr[0] error:nil];
      }else{
        topicModel = [[TopicModel alloc] initWithDictionary:resultObject error:nil];
      }
    } else {
      [self setLastError:[NSError errorWithDomain:description code:code userInfo:nil]];
    }

    completion(topicModel);
  };

  error_t _error = ^(NSError *error) {
    [self setLastError:error];
    completion(nil);
  };

  OrderInterface *interface = [[OrderInterface alloc] init];
  [interface addOrUpdateTopicWithTopicId:topicId topicName:topicName timeLength:timeLength topicDesc:topicDesc topicPrice:topicPrice completion:_completion error:_error];
}

//删除话题
+ (void)deleteTopicWithTopicId:(NSInteger)topicId
                    completion:(void (^)(BOOL isSucceed))completion {
  [self cleanLastError];

  completion_t _completion = ^(NSDictionary *resultObject, NSInteger code, NSString *description) {

    if (code == SUCCESS_RESULT) {
      completion(YES);
    } else {
      [self setLastError:[NSError errorWithDomain:description code:code userInfo:nil]];
      completion(NO);
    }

  };

  error_t _error = ^(NSError *error) {
    [self setLastError:error];
    completion(NO);
  };

  OrderInterface *interface = [[OrderInterface alloc] init];
  [interface deleteTopicWithTopicId:topicId completion:_completion error:_error];
}

@end

@implementation TopicData

+ (BOOL)propertyIsOptional:(NSString *)propertyName {
  return YES;
}

@end
